package com.erp.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.appengine.api.store.StoreService;
import com.alibaba.appengine.api.store.StoreServiceFactory;

/**
 * Simple demo of {@link com.alibaba.appengine.api.store.StoreService}
 * 
 * @author ding.lid
 */
public class StoreServiceDemo extends HttpServlet {
	private static final long serialVersionUID = -235936632435138478L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.getWriter().println("Begin store service test.");
		StoreService storeService = StoreServiceFactory.getStoreService();

		final String testText = "FooText";
		final String path = "/test.txt";

		response.getWriter().printf("Save text %s to file %s.\n", testText,
				path);
		storeService.saveTextFile(testText, path);

		String getText = storeService.getTextFile(path);
		response.getWriter().printf("Read text %s from file %s.\n", getText,
				path);

		if (testText.equals(getText)) {
			response.getWriter().println("Store service test ok!");
		} else {
			response.getWriter().printf("Fail to get, expect %s, actual %s!\n",
					testText, getText);
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}
